Find ASCII Value of a Character Using C++

03-11-17 Course- CPP

Every character in C++ programming is given an integer value to represent it. That integer value is known as ASCII value of that character. For example: ASCII value of 'a' is 97. When a character is stored in variable of type char, the ASCII value of character is stored instead of that character itself. For example: If you try to store character 'a' in a char type variable, ASCII value of that character is stored which is 97.

In, this program user is asked to enter a character and this program will display the ASCII value of that character.

Source Code


/* Source code to find ASCII value of a character entered by user */

#include <iostream>

using namespace std;

int main(){
 char ascii;
 cout << "Enter a character: ";
 cin >> ascii;
 cout << "Its ascii value is: " << (int) ascii << endl;
 return 0;
}

Output


Enter a character: G
Its ascii value is: 71

In this program, user is asked to enter a character and this character will be stored in variable ascii, i.e., the ASCII value of that character is stored in variable ascii. This character will then be converted into its equivalent ASCII value.